package com.hua.designpattern.behavioral.iterator;

public class ContainerArray implements ContainerInterface {

	private String[] elementData = null;

	public ContainerArray(String[] elementData) {
		super();
		this.elementData = elementData;
	}

	@Override
	public IteratorInterface getIterator() {
		return new IteratorInterface() {
			private int index = 0;

			@Override
			public boolean hasNext() {
				return (elementData == null ? false : index != elementData.length);
			}

			@Override
			public Object next() {
				return elementData[index++];
			}
		};
	}

	@Override
	public String toString() {
		StringBuffer elementStr = new StringBuffer();
		for (String element : elementData) {
			elementStr.append(element + ",");
		}

		return super.toString() + "[" + elementStr + "]";
	}

}
